import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, OneToOne, JoinColumn,  } from "typeorm";
import { ManualTimestamp } from "../decorators/manual-timestamp.decorator";
import { UserEntity } from "./user.entity";
import { CharacterAttributeEntity } from "./character-attribute.entity";
import { CharacterDetailedAttributeEntity } from "./character-detailed-attribute.entity";
import { CharacterSkillEntity } from "./character-skill.entity";
import { SocialRelationEntity } from "./social-relation.entity";
import { CharacterReligionEntity } from "./character-religion.entity";
import { CharacterResidenceEntity } from "./character-residence.entity";
import { CharacterStoneEntity } from "./character-stone.entity";
import { CharacterResourcesEntity } from "./character-resources.entity";
import { CharacterInventoryEntity } from "./character-inventory.entity";
import { MonthlyCardEntity } from "./monthly-card.entity";
import { CharacterLocationEntity } from "./character-location.entity";
import { CharacterGemInventoryEntity } from "./character-gem-inventory.entity";
import { EquipmentGemSocketEntity } from "./equipment-gem-socket.entity";
import { CharacterAttributePointsEntity } from "./character-attribute-points.entity";
import { CharacterEquipmentEntity } from "./character-equipment.entity";

@Entity("characters")
@ManualTimestamp()
export class CharacterEntity {
  @PrimaryGeneratedColumn({
    comment: "角色ID，从2001开始自增",
  })
  id!: number;

  @Column("int", { name: "userId" })
  userId!: number;

  @Column("varchar", { length: 32, comment: "角色名称" })
  name!: string;

  @Column("int", { comment: "职业类型(1:武士,2:文人,3:异师)" })
  roleType!: number;

  @Column("tinyint", { default: 1, comment: "性别(1:男,0:女)" })
  sex!: number;

  @Column("int", { default: 1, comment: "角色等级" })
  lvl!: number;

  @Column("int", { default: 0, comment: "当前经验值" })
  exp!: number;

  @Column("int", { default: 0, comment: "转职次数" })
  trans!: number;

  @Column("varchar", { length: 64, nullable: true, comment: "称号" })
  title!: string;

  @Column("int", { default: 0, comment: "VIP等级" })
  vip!: number;

  @Column("varchar", { length: 128, nullable: true, comment: "头像图片" })
  headImg!: string;

  @Column("tinyint", { default: 1, comment: "是否允许登录(1:允许,0:禁止)" })
  allowLogin!: number;

  @Column("tinyint", { default: 0, comment: "角色类型(0:主将,1:副将,2:其他)" })
  objType!: number;

  @Column("int", { default: 0, comment: "角色索引ID" })
  indexID!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;

  @Column("datetime", { name: "updated_at", nullable: true, comment: "更新时间" })
  updated_at!: Date | null;

  // 关联关系
  @ManyToOne(() => UserEntity, (user) => user.characters)
  @JoinColumn({ name: "userId" })
  user!: UserEntity;

  @OneToOne(() => CharacterAttributeEntity, (attr) => attr.character)
  attributes!: CharacterAttributeEntity;

  @OneToOne(() => CharacterDetailedAttributeEntity, (attr) => attr.character)
  detailedAttributes!: CharacterDetailedAttributeEntity;

  @OneToMany(() => CharacterSkillEntity, (skill) => skill.character)
  skills!: CharacterSkillEntity[];

  @OneToOne(() => SocialRelationEntity, (relation) => relation.character)
  socialRelations!: SocialRelationEntity;

  @OneToOne(() => CharacterReligionEntity, (religion) => religion.character)
  religion!: CharacterReligionEntity;

  @OneToOne(() => CharacterResidenceEntity, (residence) => residence.character)
  residence!: CharacterResidenceEntity;

  @OneToMany(() => CharacterStoneEntity, (stone) => stone.character)
  stones!: CharacterStoneEntity[];

  @OneToOne(() => CharacterResourcesEntity, (resources) => resources.character)
  resources!: CharacterResourcesEntity;

  @OneToOne(() => CharacterInventoryEntity, (inventory) => inventory.character)
  inventory!: CharacterInventoryEntity;

  @OneToMany(() => MonthlyCardEntity, (monthlyCard) => monthlyCard.character)
  monthlyCards!: MonthlyCardEntity[];

  // 位置关联
  @OneToOne(() => CharacterLocationEntity, (location) => location.character)
  location!: CharacterLocationEntity;

  // 宝石系统关联
  @OneToMany(
    () => CharacterGemInventoryEntity,
    (gemInventory) => gemInventory.character
  )
  gemInventories!: CharacterGemInventoryEntity[];

  @OneToMany(() => EquipmentGemSocketEntity, (gemSocket) => gemSocket.character)
  equipmentGemSockets!: EquipmentGemSocketEntity[];

  // 属性点分配关联
  @OneToOne(
    () => CharacterAttributePointsEntity,
    (attrPoints) => attrPoints.character
  )
  attributePoints!: CharacterAttributePointsEntity;

  // 装备穿戴关联
  @OneToOne(() => CharacterEquipmentEntity, (equipment) => equipment.character)
  equipment!: CharacterEquipmentEntity;

  // 配偶和师傅关系（通过ID关联）
  @ManyToOne(() => CharacterEntity, { nullable: true })
  @JoinColumn({ name: "spouseId" })
  spouse!: CharacterEntity;

  @ManyToOne(() => CharacterEntity, { nullable: true })
  @JoinColumn({ name: "masterId" })
  master!: CharacterEntity;
}
